using IQIGame.Onigao.Framework;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.Game
{
    public class CacheUI : IReusableClass
    {
        public Type windowType;
        public UIBaseWindow activeWindow;
        public Func<bool> delegateNeedRecover;

        public UIBaseData leaveData;
        public List<CacheUI> fullScreenOperateList;

        public uint MaxStore => 50;

        public static CacheUI GetFromPool()
        {
            return ClassPool<CacheUI>.Get();
        }

        public CacheUI Sync(Type windowType, UIBaseWindow activeWindow)
        {
            this.windowType = windowType;
            this.activeWindow = activeWindow;
            fullScreenOperateList = ListPool<CacheUI>.Get();
            UIBaseWindow.NeedRecoverDelegates.TryGetValue(windowType, out delegateNeedRecover);
            return this;
        }

        public void RecordLeaveData(UIBaseData leaveData)
        {
            this.leaveData = leaveData;
        }

        /// <summary>
        /// 纪录全屏UI关闭的UI
        /// </summary>
        /// <param name="list"></param>
        public void RecordOperateList(List<CacheUI> list)
        {
            this.fullScreenOperateList.AddRange(list);
        }

        /// <summary>
        /// 检查目前这个UI是否需要还原
        /// 根据每个UI对应的委托，该值可能不同，默认委托==null，需要还原
        /// 比如：任务跟踪UI仅在当前有跟踪目标时才需要还原，如果关闭UI的过程中取消了跟踪目标，则还原时可忽略该UI
        /// </summary>
        /// <returns></returns>
        public bool CheckNeedRecover()
        {
            var func = this.delegateNeedRecover;
            this.delegateNeedRecover = null;
            if (func != null)
                return func();
            return true;
        }

        /// <summary>
        /// 清除内部所有集合与需要Dispose或Release的对象
        /// </summary>
        public void Put2Pool()
        {
            ClassPool<CacheUI>.Put(this);
        }

        public void OnReset()
        {
            windowType = null;
            activeWindow = null;
            delegateNeedRecover = null;

            if (fullScreenOperateList != null)
            {
                ListPool<CacheUI>.Put(fullScreenOperateList);
                fullScreenOperateList = null;
            }
            leaveData = null;
        }
    }
}
